package gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import client.libmessenger.*;

public class LoginWindow extends JFrame implements WindowListener
{
	// data structures
	public static LoginWindow instance = null;
	private LibMessenger libMessage = null;
	
	// interface elements
	private Container container;	
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	private JTextField txtUsername, txtServer;
	private JPasswordField txtPassword;	
	private JLabel lblUsername, lblServer, lblPassword, lblPicture, lblMSNClone, lblBlank;
	private JButton btnLogin;
	private JPanel mainPanel, leftPanel, rightPanel; 		
	

	public LoginWindow(){
		super("MSN Clone Login");
		instance = this;
						
		container = getContentPane();
		layout = new GridBagLayout();
		container.setLayout(layout);
		
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout(5, 5));
		leftPanel = new JPanel();
		leftPanel.setLayout(new GridLayout(3, 1, 5, 5));
		rightPanel = new JPanel();
		rightPanel.setLayout(new GridLayout(3, 1, 5, 5));
		
		constraints = new GridBagConstraints();
		
		txtUsername = new JTextField(20);
		txtServer = new JTextField(20);
		txtPassword = new JPasswordField(20);
		
		// TODO: remove default values
		txtUsername.setText("TeamBIsTheBest");
		txtPassword.setText("password");
		txtServer.setText("127.0.0.1");
		
		lblMSNClone = new JLabel("MSN Clone");
		lblUsername = new JLabel("Username:  ");
		lblPassword = new JLabel("Password:  ");
		lblServer = new JLabel("Server:  ");
		lblPicture = new JLabel(new ImageIcon("./Images/Welcome.png"));
		lblBlank = new JLabel("        ");
		btnLogin = new JButton("Login");
		
		lblMSNClone.setHorizontalAlignment(JLabel.CENTER);
		lblUsername.setHorizontalAlignment(JLabel.RIGHT);
		lblPassword.setHorizontalAlignment(JLabel.RIGHT);
		lblServer.setHorizontalAlignment(JLabel.RIGHT);
		
		constraints.ipadx = 8;
		constraints.ipady = 8;

		// add the labels	
		lblMSNClone.setFont(new Font("Arial", Font.BOLD, 36));
		
		btnLogin.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                btnLoginMouseClicked(evt);
            }
        });

	
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 0;
		constraints.gridy = 0;	
		constraints.gridwidth = 4;
		constraints.gridheight = 1;
		container.add(lblMSNClone, constraints);				
		
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridx = 0;
		constraints.gridy = 1;	
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		container.add(lblUsername, constraints);

		constraints.gridx = 0;
		constraints.gridy = 2;		
		container.add(lblPassword, constraints);
						
		constraints.gridx = 0;
		constraints.gridy = 3;		
		container.add(lblServer, constraints);
		
		constraints.gridx = 5;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.gridheight = 5;
		container.add(lblPicture, constraints);
		
		// add the text boxes
		constraints.gridx = 1;
		constraints.gridy = 1;		
		constraints.gridwidth = 3;
		constraints.gridheight = 1;
		container.add(txtUsername, constraints);
				
		constraints.gridx = 1;
		constraints.gridy = 2;		
		container.add(txtPassword, constraints);
		
		constraints.gridx = 1;
		constraints.gridy = 3;		
		container.add(txtServer, constraints);
				
		constraints.gridx = 3;
		constraints.gridy = 4;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.NONE;
		btnLogin.setAlignmentX(JButton.CENTER);
		container.add(btnLogin, constraints);
		
		this.addWindowListener(this);
		this.pack();
		this.setSize((int) this.getSize().getWidth()+5, (int) getSize().getHeight()+5);
		this.setResizable(false);
	}
	
	public void 	windowClosed(WindowEvent e){}
   public void 	windowActivated(WindowEvent e){}
   public void 	windowClosing(WindowEvent e){ System.exit(0); }
   public void 	windowDeactivated(WindowEvent e) {}
   public void 	windowDeiconified(WindowEvent e) {}
   public void 	windowIconified(WindowEvent e) {}
   public void 	windowOpened(WindowEvent e) {}   	
	
	private void btnLoginMouseClicked(MouseEvent evt) 
	{
        String username = "", password = "", server = "";
        libMessage = new LibMessenger();
        
        if ((username = txtUsername.getText()).equals(""))
            JOptionPane.showMessageDialog(null, "Please enter your username.", "Missing Username", JOptionPane.ERROR_MESSAGE);
        else if ((password = txtPassword.getText()).equals(""))
             JOptionPane.showMessageDialog(null, "Please enter your password.", "Missing Password", JOptionPane.ERROR_MESSAGE);
        else if ((server = txtServer.getText()).equals(""))
             JOptionPane.showMessageDialog(null, "Please enter the server address", "Missing Server", JOptionPane.ERROR_MESSAGE);
        else 
        {
             if (libMessage.connect(server, username, password))
             {
                 new MessengerWindow(libMessage, username).setVisible(true);
                 this.setVisible(false);
             } // IF
             else
            	 JOptionPane.showMessageDialog(null, "Connection failed: "+libMessage.lastError, "Connection Error", JOptionPane.ERROR_MESSAGE);
        } // ELSE                                
    }
	public static void main(String[] args)
	{
		new LoginWindow().setVisible(true);
	}
}